home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / zcpp_jae.zip / SAMPLES.CPP < prev    next >
C/C++ Source or Header  |  1991-04-28  |  4KB  |  189 lines

  1. #include <assert.h>
  2.  
  3. // These pragmas must be defined so that templates may be expanded.
  4. //
  5. #pragma defmacro MACRO "macro" delimiter=} recursive
  6. #pragma defmacro template "template" delimiter=}
  7. #pragma defmacro DECLARE "declare" delimiter=> recursive lines
  8. #pragma defmacro IMPLEMENT "implement" delimiter=> recursive lines
  9.  
  10. #ifndef NULL
  11. #   if sizeof (long) == sizeof (void *)
  12. #       define NULL 0L
  13. #   else
  14. #       define NULL 0
  15. #   endif
  16. #endif
  17.  
  18. //------------------------------------------------------------------------
  19. // Your basic ring buffer.
  20. //
  21. enum FifoStatus {Empty, HasData, Overflow};
  22.  
  23. template<class T> class Fifo<T> {
  24.     T *buffer;
  25.     unsigned size;
  26.     unsigned head;
  27.     unsigned tail;
  28.     unsigned overflow;
  29.     unsigned advance (unsigned i) { return (i+1) % size; }
  30. public:
  31.     Fifo<T> (unsigned sz);
  32.     ~Fifo<T> ();
  33.     FifoStatus status ();
  34.     FifoStatus get (T &e);
  35.     void put (T &e);
  36. };
  37.  
  38.  
  39. template<class T> Fifo<T>::Fifo<T> (unsigned sz) {
  40.     tail = overflow = 0;
  41.     head = tail + 1;
  42.     buffer = (T *)new T[size = sz];
  43.     assert (buffer != NULL);
  44. }
  45.  
  46.  
  47. template<class T> Fifo<T>::~Fifo<T> () { delete buffer; }
  48.  
  49.  
  50. template<class T> FifoStatus Fifo<T>::status () {
  51.     if (overflow) {
  52.         overflow = 0;
  53.         return Overflow;
  54.     }
  55.     if (advance (tail) == head)
  56.         return Empty;
  57.     return HasData;
  58. }
  59.  
  60.  
  61. template<class T> FifoStatus Fifo<T>::get (T &e) {
  62.     unsigned t_indx = advance (tail);
  63.     if (t_indx == head)
  64.         return Empty;
  65.     e = buffer[tail = t_indx];
  66.     return HasData;
  67. }
  68.  
  69. template<class T> void Fifo<T>::put (T &e) {
  70.     if (head == tail)
  71.         overflow++;
  72.     else {
  73.         buffer[head] = e;
  74.         head = advance (head);
  75.     }
  76. }
  77.  
  78. DECLARE Fifo<int>;
  79. IMPLEMENT Fifo<int>;
  80.  
  81. Fifo<int> myfifo (32);
  82.  
  83.  
  84.  
  85. // Here is some stuff pulled directly from the ARM and converted for cpp's
  86. // syntax.
  87. //
  88.  
  89. class Complex {
  90.     float re, im;
  91. public:
  92.     Complex ();
  93.     // ...
  94. };
  95.  
  96. template<class T> class Vector<T> {
  97.     T* v;
  98.     int sz;
  99. public:
  100.     Vector<T> (int);
  101.     T& elem (int i) { return v[i]; }
  102.     T& operator[] (int i);
  103.     //...
  104. };
  105.  
  106. DECLARE Vector<void *>;
  107.  
  108. template<class T> class Pvector<T>: Vector<void *> {
  109. public:
  110.     Pvector<T> (int i): (i) {}
  111.     T*& elem (int i)
  112.         { return (T*&) Vector<void *>::elem (i); }
  113.     T*& operator[] (int i)
  114.         { return (T*&) Vector<void *>::operator[] (i); }
  115.     // ....
  116. };
  117.  
  118. DECLARE Pvector<int *>;
  119. DECLARE Pvector<Complex *>;
  120. DECLARE Pvector<char *>;
  121.  
  122.  
  123. // Using templates to build ``type'' shells around a general purpose class.
  124. // A redo of Stroustrup's slist class from _The C++ Programming Language_
  125. //
  126.  
  127. // The general purpose class.  Specific purpose classes will be derived from
  128. // this one.
  129. //
  130. class Slist;
  131.  
  132. class Slink { friend class Slist;  // friend class Slist_iterator;
  133.     Slink *next;
  134.     void *e;
  135.     Slink (void *a, Slink *p): e(a), next(p) {}
  136. };
  137.  
  138. class Slist { // friend class Slist_iterator;
  139.     Slink *tail;
  140. #   define head tail->next
  141. public:
  142.     Slist (): tail(NULL) {}
  143.     Slist (void *a) {
  144.         tail = (Slink *)new Slink (a, NULL);
  145.         assert (tail != NULL);
  146.         head = tail;
  147.     }
  148.     ~Slist () { clear (); }
  149.     int insert (void *a);
  150.     int append (void *a);
  151.     void *get ();
  152.     void clear ();
  153. };
  154.  
  155.  
  156. // Now for the template class.
  157. //
  158. template<class T> class GSlist<T>: Slist {
  159. public:
  160.     GSlist<T> () {}
  161.     GSlist<T> (T *a): Slist ((void *)a) {}
  162.     ~GSlist<T> () { clear (); }
  163.     int insert (T *a) { return Slist::insert ((void *)a); }
  164.     int append (T *a) { return Slist::append ((void *)a); }
  165.     T *get () { return (T *)Slist::get (); }
  166. };
  167.  
  168. struct Employee {
  169.     char *name;
  170.     char *socSec;
  171.     int payScale;
  172.     // ...
  173. };
  174.  
  175. struct Card {
  176.     int rank;
  177.     int suit;
  178. };
  179.  
  180. // Declarations.
  181. //
  182. DECLARE GSlist<Employee>;
  183. DECLARE GSlist<Card>;
  184.  
  185. // Definitions.
  186. //
  187. GSlist<Employee> employeeList;
  188. GSlist<Card>     cardList;
  189.